package com.xinke.edu.mypetadoption.fragment3;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.databinding.FragmentSjBinding;
import com.xinke.edu.mypetadoption.databinding.FragmentTjBinding;
import com.xinke.edu.mypetadoption.entity.DashboardDTO;
import com.xinke.edu.mypetadoption.entity.OrdersUserProductDTO;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;

import java.math.BigDecimal;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class SjFragment extends Fragment {

    //数据页面

    FragmentSjBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentSjBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loaddata();

    }


    private void loaddata(){


        RetrofitUtil.getApi().getDashboard()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<DashboardDTO>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody<DashboardDTO> result) {

                        DashboardDTO dashboardDTO = result.getData();

                        if (dashboardDTO != null){
                            binding.byxse.setText(String.format("%.2f$", dashboardDTO.getTotalSales()));
                            binding.bydd.setText(String.valueOf(dashboardDTO.getOrderCount()));
                            binding.bysj.setText(String.valueOf(dashboardDTO.getMerchantCount()));
                            binding.byyh.setText(String.valueOf(dashboardDTO.getUserCount()));

                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(requireContext(), "加载失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}